/* Copyright 2012 Nathan Begle
    This file is part of pcoursemanager.

    pcoursemanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pcoursemanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pcoursemanager.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;

import javax.swing.GroupLayout;


public class Course_Window {
	Frame frame;
	Label title_lbl;
	
	Label cap_txt_lbl;
	Label act_txt_lbl;
	Label rem_txt_lbl;
	
	Label cap_lbl;
	Label act_lbl;
	Label rem_lbl;
	
	Label available_lbl;
	
	Button kill_bt;
	
	Panel main_Panel;
	Panel comp_Panel;
	
	GroupLayout main_Layout;
	GroupLayout comp_Layout;
	
	public Course_Window(WindowListener wl, ActionListener al, String courseString, int year, int semester, long crn, int capacity, int actual, int remaining)
	{
		frame = new Frame(courseString);
		frame.addWindowListener(wl);
		
		main_Panel = new Panel();
		comp_Panel = new Panel();
		
		main_Layout = new GroupLayout(main_Panel);
		main_Panel.setLayout(main_Layout);
		main_Layout.setAutoCreateGaps(true);
		
		comp_Layout = new GroupLayout(comp_Panel);
		comp_Panel.setLayout(comp_Layout);
		comp_Layout.setAutoCreateGaps(true);
		
		title_lbl = new Label(courseString+":");
		//title_lbl.setPreferredSize(new Dimension(200, 20));
		cap_txt_lbl = new Label("Capacity:");
		//cap_txt_lbl.setPreferredSize(new Dimension(100, 20));
		act_txt_lbl = new Label("Actual:");
		//act_txt_lbl.setPreferredSize(new Dimension(100, 20));
		rem_txt_lbl = new Label("Remaining:");
		//rem_txt_lbl.setPreferredSize(new Dimension(100, 20));
		
		cap_lbl = new Label(Integer.toString(capacity));
		//cap_lbl.setPreferredSize(new Dimension(100, 20));
		act_lbl = new Label(Integer.toString(actual));
		//act_lbl.setPreferredSize(new Dimension(100, 20));
		rem_lbl = new Label(Integer.toString(remaining));
		//rem_lbl.setPreferredSize(new Dimension(100, 20));
		
		available_lbl = new Label();
		if(remaining > 0) updateAvailability(true);
		else updateAvailability(false);
		
		kill_bt = new Button("Quit Course");
		kill_bt.setActionCommand(courseString+"_bt");
		kill_bt.addActionListener(al);
		title_lbl.setPreferredSize(new Dimension(200, 30));
		
		setCompLayout();
		setMainLayout();
		
		frame.add(main_Panel);
		frame.pack();
		frame.setLocation(new Point(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - frame.getSize().width / 2,
				Toolkit.getDefaultToolkit().getScreenSize().height / 2 - frame.getSize().height / 2));
		frame.setVisible(true);
	}
	
	void setCompLayout()
	{
		comp_Layout.setVerticalGroup(comp_Layout.createSequentialGroup()
				.addGroup(comp_Layout.createParallelGroup()
						.addComponent(cap_txt_lbl).addComponent(cap_lbl))
				.addGroup(comp_Layout.createParallelGroup()
						.addComponent(act_txt_lbl).addComponent(act_lbl))
				.addGroup(comp_Layout.createParallelGroup()
						.addComponent(rem_txt_lbl).addComponent(rem_lbl)));
		comp_Layout.setHorizontalGroup(comp_Layout.createSequentialGroup()
				.addGroup(comp_Layout.createParallelGroup()
						.addComponent(cap_txt_lbl).addComponent(act_txt_lbl).addComponent(rem_txt_lbl))
				.addGroup(comp_Layout.createParallelGroup()
						.addComponent(cap_lbl).addComponent(act_lbl).addComponent(rem_lbl)));
	}
	
	void setMainLayout()
	{
		main_Layout.setVerticalGroup(main_Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(title_lbl))
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(comp_Panel))
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(available_lbl))
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(kill_bt)).addContainerGap());
		main_Layout.setHorizontalGroup(main_Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(main_Layout.createParallelGroup()
						.addComponent(title_lbl).addComponent(comp_Panel).addComponent(available_lbl).addComponent(kill_bt))
				.addContainerGap());
	}
	
	void updateAvailability(boolean available)
	{
		if(available)
		{
			Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_available.png"));
			frame.setIconImage(image);
			available_lbl.setText("Available");
			available_lbl.setForeground(Color.GREEN);
		}
		else
		{
			Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Content/pcm_not_available.png"));
			frame.setIconImage(image);
			available_lbl.setText("Not Available");
			available_lbl.setForeground(Color.RED);
		}
		//available_lbl.setPreferredSize(new Dimension(200, 20));
	}
	
	public void update(int capacity, int actual, int remaining)
	{
		cap_lbl.setText(Integer.toString(capacity));
		act_lbl.setText(Integer.toString(actual));
		rem_lbl.setText(Integer.toString(remaining));
		
		if(remaining > 0) updateAvailability(true);
		else updateAvailability(false);
	}
	
	public void removeWindow()
	{
		frame.dispose();
	}
	
	public void bring_to_front()
	{
		frame.toFront();
	}
}
